﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DMS.DAL;
using DMS.Logic;
namespace WebUI.library
{
    public partial class permission : System.Web.UI.Page
    {
        int documentLibId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                documentLibId = int.Parse(Request.QueryString["id"]);
            }
            if (!IsPostBack)
            {
                if (documentLibId > 0)
                {
                    loadGvUser();
                    loaddrpUsers();
                }
            }
        }
        void loaddrpUsers()
        {
            List<User> usr = BL.getInstance().getAllUser();
            DropDownList drp = (DropDownList)gvUser.FooterRow.FindControl("drpUser");
            drp.DataSource = usr;
            drp.DataTextField = "userName";
            drp.DataValueField= "ID";
            drp.DataBind();

        }
        void loadGvUser()
        {
            List<UserPermission> up = BL.getInstance().getUserPermissionsForDocumentLibrary(documentLibId);
            if (up==null)
            {
                up = new List<UserPermission>();
                up.Add(new UserPermission());
            }
            gvUser.DataSource = up;
            gvUser.DataBind();
        }
        void cancelGvUser()
        {
            gvUser.EditIndex = -1;
            gvUser.ShowFooter = true;
            loadGvUser();
        }
        protected void gvUser_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

        protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState != (DataControlRowState.Alternate | DataControlRowState.Edit) && (e.Row.RowState != (DataControlRowState.Normal | DataControlRowState.Edit)) && (e.Row.RowState != (DataControlRowState.Alternate | DataControlRowState.Insert) && (e.Row.RowState != (DataControlRowState.Normal | DataControlRowState.Insert))))))
            {
                User user = (User)DataBinder.Eval(e.Row.DataItem, "User");
                if (user == null)
                    e.Row.Visible = false;

            }
            else if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState != (DataControlRowState.Alternate | DataControlRowState.Edit) || (e.Row.RowState != (DataControlRowState.Normal | DataControlRowState.Edit))))
            {

            }
        }

        protected void gvUser_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "add")
            {
                BL.getInstance().saveUserPermission();
            }
        }
    }
}
